package com.lcxw.hxzbapi.controller.roomcontroller;


import com.alibaba.fastjson.JSONObject;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.AttentionService;
import com.lcxw.hxzbapi.service.YwpdRoomService;

@RestController
@RequestMapping("/api/room/attention")
@CrossOrigin
@Slf4j
public class RoomAttentionController {

    @Resource
    AttentionService attentionService;

    @Resource
    YwpdRoomService ywpdRoomService;

    @Resource
    RoomRealization roomRealization;

    /**
     * 1、添加一个关注房间
     */
    @PostMapping("/add")
    public Response addRoomAtt(@RequestBody JSONObject requestParams) {
        long attuId = Long.parseLong(requestParams.getString("attRoomId"));
        long userId = Long.parseLong(requestParams.getString("userId"));
        try {
            roomRealization.addAttRoom(userId, attuId);
        } catch (Exception e) {
            return Response.dbFail(log, "关注房间失败，数据库插入失败");
        }
        return Response.ok();
    }

    /**
     * 2、获取用户关注的所有房间
     */
    @PostMapping("/get")
    public Response getAttRoom(@RequestBody JSONObject requestParams) {
        long userId = Long.parseLong(requestParams.getString("userId"));
        List<Long> attRoomIdList;
        try {
            attRoomIdList = attentionService.getAttentionRoombyUserId(userId);
        } catch (Exception e) {
            return Response.dbFail(log,"查询失败，数据库操作失败");
        }
        List<JSONObject> data = roomRealization.getAttRoomInfo(attRoomIdList);
        return Response.ok(data);

    }

    /**
     * 3、取消关注一个房间
     */
    @PostMapping("/delete")
    public Response deleteAttRoom(@RequestBody JSONObject requestParams) {
        long attuId = Long.parseLong(requestParams.getString("attRoomId"));
        long userId = Long.parseLong(requestParams.getString("userId"));
        try {
            roomRealization.deleteAttRoom(userId, attuId);
            return Response.ok();
        } catch (Exception e) {
            return Response.dbFail(log, "取消关注失败，数据库操作失败");
        }
    }


}
